<?php

namespace app\merchant\controller\goods;

use app\merchant\controller\Base;
use app\merchant\model\Spec as SpecModel;
use app\merchant\model\SpecValue as SpecValueModel;

/**
 * 商品规格控制器
 * Class Spec
 * @package app\store\controller
 */
class Spec extends Base
{
    /* @var SpecModel $SpecModel */
    private $SpecModel;

    /* @var SpecValueModel $SpecModel */
    private $SpecValueModel;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->SpecModel = new SpecModel;
        $this->SpecValueModel = new SpecValueModel;
    }

    /**
     * 添加规则组
     * @param $spec_name
     * @param $spec_value
     * @return array
     */
    public function addSpec()
    {
        $param = $this->postData();

        if(!isset($param['spec_name']) || empty($param['spec_name'])){
            return $this->renderError([],'请输入规格值');
        }
        if(!isset($param['spec_value']) || empty($param['spec_value'])){
            return $this->renderError([],'请输入规格值');
        }

        // 判断规格组是否存在
        if (!$specId = $this->SpecModel->getSpecIdByName($param['spec_name'])) {
            // 新增规格组and规则值
            if ($this->SpecModel->add($param['spec_name']) && $this->SpecValueModel->add($this->SpecModel['spec_id'], $param['spec_value']))
                return $this->renderSuccess([
                    'spec_id' => (int)$this->SpecModel['spec_id'],
                    'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
                ]);
            return $this->renderError();
        }
        // 判断规格值是否存在
        if ($specValueId = $this->SpecValueModel->getSpecValueIdByName($specId, $param['spec_value'])) {
            return $this->renderSuccess([
                'spec_id' => (int)$specId,
                'spec_value_id' => (int)$specValueId,
            ]);
        }

        // 添加规则值
        if ($this->SpecValueModel->add($specId, $param['spec_value']))
            return $this->renderSuccess([
                'spec_id' => (int)$specId,
                'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
            ]);
        return $this->renderError();
    }


    /**
     * 添加规格值
     * @param $spec_id
     * @param $spec_value
     * @return array
     */
    public function addSpecValue($spec_id, $spec_value)
    {
        $param = $this->postData();
        if(!isset($param['spec_id']) || empty($param['spec_id'])){
            return $this->renderError([],'参数错误');
        }

        if(!isset($param['spec_value']) || empty($param['spec_value'])){
            return $this->renderError([],'请输入规格值');
        }
        //判断规格组是否存在
        if (!$spec = $this->SpecModel->getSpecIdById($spec_id)) {

            return $this->renderError([],'规格组不存在,请刷新页面重试');

        }

        // 判断规格值是否存在
        if ($specValueId = $this->SpecValueModel->getSpecValueIdByName($spec_id, $spec_value)) {
            return $this->renderSuccess([
                'spec_value_id' => (int)$specValueId,
            ]);
        }
        // 添加规则值
        if ($this->SpecValueModel->add($spec_id, $spec_value))
            return $this->renderSuccess([
                'spec_value_id' => (int)$this->SpecValueModel['spec_value_id'],
            ]);

    }

}
